<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

require_once APPPATH . 'controllers/admin.php';

/**
 * Контроллер изменения основных шаблонов
 *
 * @package CoreFramework
 * @version 1.0
 * @author Gennadiy Kozlenko
 */
class Admin_Edit_Layouts extends Admin {
   
   /**
    * Массив лайаутов, исключаемых из редактирования
    *
    * @access private
    * @var array
    */
   var $_excludes = array('admin', 'empty', 'content');
   
   /**
    * Конструктор класса
    *
    * @access public
    * @return Admin_Edit_Layouts
    */
   function Admin_Edit_Layouts() {
      parent::Admin();
      $this->set_menu(2, 4);
   } // end Admin_Edit_Layouts
   
   /**
    * Метод по умолчанию
    *
    * @access public
    */
   function index() {
      $data = array();
      $this->load->helper('file');
      // Получаем список лайаутов
      $layouts = array();
      foreach (get_filenames(APPPATH . 'views/' . $this->config->item('language') . '/layouts') as $file) {
         $name = preg_replace('~\.html$~', '', $file);
         if (!in_array($name, $this->_excludes)) {
            $layouts[$name] = str_replace('{%layout%}', ucfirst($name), $this->lang->line('template_item'));
         }
      }
      if (false !== $this->input->post('layout')) {
         $layout = basename(trim($this->input->post('layout')));
      } elseif (false !== $this->input->get('layout')) {
         $layout = basename(trim($this->input->get('layout')));
      } else {
         $layout = current(array_keys($layouts));
      }
      $content = '';
      if (false !== $this->input->post('edit')) {
         
         $content = $this->input->post('content');
         // Сохраняем шаблон
         if (write_file(APPPATH . 'views/' . $this->config->item('language') . '/layouts/' . $layout . '.html', $content)) {
            $this->set_message($this->lang->line('edit_layout_succ'), 'info', true);
            $this->redirect($this->get_controller(), 'index', array('layout' => $layout));
         }
         $this->set_message($this->lang->line('edit_layout_error'), 'error');
      } else {
         $content = read_file(APPPATH . 'views/' . $this->config->item('language') . '/layouts/' . $layout . '.html');
      }
      // Забиваем в дату поля
      $this->load->helper('element');
      $data['layouts'] = create_select('layout', $layouts, $layout, 'class="i" onchange="this.form.submit()"');
      // Защищаем специальные теги
      $content = str_replace(array('{%', '%}'), array('{-%', '%-}'), $content);
      $data['content'] = htmlspecialchars($content);
      // Выводим
      $this->set_content($this->parser->parse('admin/edit_layouts.html', $data, true));
      $this->display();
   } // end index
   
}
